﻿using FreeSql.DataAnnotations;
using GalaSoft.MvvmLight;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing.Printing;
using System.Linq;
using System.Printing;
using System.Security.Principal;
using System.Text;
using System.Windows.Controls;
using System.Xml.Linq;
namespace sys_Print.DataAsset
{
    [JsonObject(MemberSerialization.OptIn)]
    public partial class PrintModelType : ObservableObject
    {
        [JsonProperty, Column(Name = "id", IsPrimary = true, IsIdentity = true)]
        public int Id { get; set; }

        [JsonProperty, Column(Name = "height")]
        public double Height { get; set; }

        [JsonProperty, Column(Name = "name", DbType = "varchar(255)")]
        public string Name { get; set; }

        [JsonProperty, Column(Name = "usage_scenario", DbType = "varchar(255)")]
        public string UsageScenario { get; set; }

        [JsonProperty, Column(Name = "width")]
        public double Width { get; set; }

        [JsonProperty, Column(Name = "state")]
        public bool State { get; set; }

        [JsonProperty, Column(Name = "printer_name", DbType = "varchar(255)")]
        public string PrinterName { get; set; } = string.Empty;

        [JsonProperty, Column(Name = "copies")]
        public int Copies { get; set; }

        [JsonProperty, Column(Name = "max_row_Height")]
        public double? MaxRowHeight { get; set; }

        [Column(IsIgnore = true)]
        public bool IsChanged { get; set; }


        public void AcceptChanges()
        {
            IsChanged = false;
        }

        /// <summary>
        /// 模板打印机参数设置
        /// </summary>
        /// <param name="printDialog">打印机对象</param>
        /// <returns></returns>
        public PrintDialog GetPrintDialog(PrintDialog printDialog)
        {
            // 获取打印选项
            PrintTicket printTicket = printDialog.PrintQueue.DefaultPrintTicket;

            // 设置打印份数
            printTicket.CopyCount = this.Copies;

            //设置边距模式
            printTicket.PageBorderless = PageBorderless.Borderless;

            ////设置纸张大小
            double width = this.Width / 25.4 * 96;
            double height = this.Height / 25.4 * 96;

            printTicket.PageMediaSize = new PageMediaSize(width, height);

            ////横向打印
            //printTicket.PageOrientation = PageOrientation.Landscape;

            // 将打印选项应用到打印对话框
            printDialog.PrintTicket = printTicket;

            return printDialog;
        }

        /// <summary>
        /// 根据打印类型与生产任务单确认映射模板
        /// </summary>
        /// <param name="taskInfo">任务对象</param>
        public ContentTemplateMapping GetContentTemplateMapping(TaskInfo taskInfo,int CanvasId)
        {
            IFreeSql _fsql = App.FsqlDB;
            //客户、系列全部符合
            ContentTemplateMapping contentTemplateMapping = _fsql.Select<ContentTemplateMapping>()
                                                        .Where(a => a.PrintModelId == this.Id)
                                                        .Where(a => a.CanvasId == CanvasId)
                                                        .Where(a => a.CustomId.Contains(taskInfo.CustomerNo))
                                                        .Where(a => a.ProductSeriesId.Contains(taskInfo.FNumber.Substring(2, 2)))
                                                        .ToOne();

            //系列符合
            if (contentTemplateMapping == null)
            {
                contentTemplateMapping = _fsql.Select<ContentTemplateMapping>()
                            .Where(a => a.PrintModelId == this.Id)
                            .Where(a => a.CanvasId == CanvasId)
                            .Where(a => a.CustomId.Contains("001"))
                            .Where(a => a.ProductSeriesId.Contains(taskInfo.FNumber.Substring(2, 2)))
                            .ToOne();
            }

            //客户符合
            if (contentTemplateMapping == null)
            {
                contentTemplateMapping = _fsql.Select<ContentTemplateMapping>()
                                            .Where(a => a.PrintModelId == this.Id)
                                            .Where(a => a.CanvasId == CanvasId)
                                            .Where(a => a.CustomId.Contains(taskInfo.CustomerNo))
                                            .Where(a => a.ProductSeriesId.Contains("00"))
                                            .ToOne();
            }

            //全部不符合
            if (contentTemplateMapping == null)
            {
                contentTemplateMapping = _fsql.Select<ContentTemplateMapping>()
                                            .Where(a => a.PrintModelId == this.Id)
                                            .Where(a => a.CanvasId == CanvasId)
                                            .Where(a => a.CustomId.Contains("001"))
                                            .Where(a => a.ProductSeriesId.Contains("00"))
                                            .ToOne();
            }

            return contentTemplateMapping;
        }

        /// <summary>
        /// 根据打印类型和任务对象判断是否双面
        /// </summary>
        /// <param name="taskInfo"></param>
        /// <returns></returns>
        public bool IsTwoSided(TaskInfo taskInfo)
        {
            IFreeSql _fsql = App.FsqlDB;

            //客户、系列全部符合
            List<ContentTemplateMapping> mappings = _fsql.Select<ContentTemplateMapping>()
                                                        .Where(a => a.PrintModelId == this.Id)
                                                        .Where(a => a.CustomId.Contains(taskInfo.CustomerNo))
                                                        .Where(a => a.ProductSeriesId.Contains(taskInfo.FNumber.Substring(2, 2)))
                                                        .ToList();
            //系列符合
            if (mappings.Count == 0)
            {
                mappings = _fsql.Select<ContentTemplateMapping>()
                                .Where(a => a.PrintModelId == this.Id)
                                .Where(a => a.CustomId.Contains("001"))
                                .Where(a => a.ProductSeriesId.Contains(taskInfo.FNumber.Substring(2, 2)))
                                .ToList();
            }

            //客户符合
            if (mappings.Count == 0)
            {
                mappings = _fsql.Select<ContentTemplateMapping>()
                                .Where(a => a.PrintModelId == this.Id)
                                .Where(a => a.CustomId.Contains(taskInfo.CustomerNo))
                                .Where(a => a.ProductSeriesId.Contains("00"))
                                .ToList();
            }


            //全部不符合
            if (mappings.Count == 0)
            {
                mappings = _fsql.Select<ContentTemplateMapping>()
                                .Where(a => a.PrintModelId == this.Id)
                                .Where(a => a.CustomId.Contains("001"))
                                .Where(a => a.ProductSeriesId.Contains("00"))
                                .ToList();
            }

            List<PrintCanvasModel> printCanvasModels = _fsql.Select<PrintCanvasModel>()
                                                            .Where(a => a.ModelId == this.Id)
                                                            .Where(a => a.IsReverse == true)
                                                            .Where(a => a.ControlType == "DataGrid")
                                                            .ToList();

            foreach (PrintCanvasModel item in printCanvasModels)
            {
                foreach (ContentTemplateMapping mapping in mappings)
                {
                    if (item.ProjectId == mapping.CanvasId)
                    {
                        return true;
                    }
                }
            }
            return false;
        }
    }
}
